//
//  HYStoreListOrder.m
//  HaiYue
//
//  Created by skyline on 2018/7/2.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYStoreListOrder.h"
#import "HYStoreListModel.h"
@implementation HYStoreListOrder
+ (NSMutableDictionary *)order:(id)dict
{
    
   NSMutableDictionary *new_dict = [NSMutableDictionary dictionary];
    
    //未按vip等级分类的list --> 按vip等级分类的list_dict
    NSMutableArray *list = (NSMutableArray *)[dict valueForKey:@"list"];
    NSMutableDictionary *list_dict = [NSMutableDictionary dictionary];
    for (NSDictionary *sub_dict in list)
    {
        NSString *vipLV = [NSString stringWithFormat:@"%@",[sub_dict valueForKey:@"viplevel"]];
        if (![list_dict valueForKey:vipLV])
        {
            HYGoodsModel *goodsmodel = [HYGoodsModel mj_objectWithKeyValues:sub_dict];
            [list_dict setValue:[NSMutableArray arrayWithObject:goodsmodel] forKey:vipLV];
        }
        else
        {
            HYGoodsModel *goodsmodel = [HYGoodsModel mj_objectWithKeyValues:sub_dict];
            
            NSMutableArray *array = [list_dict valueForKey:vipLV];
            NSMutableArray *array_copy = [array mutableCopy];
            [array_copy addObject:goodsmodel];
            [list_dict setValue:array_copy forKey:vipLV];
        }
    }
    
    //无序list_dict-->有序list_array
    NSMutableArray *list_array = [NSMutableArray array];
    NSArray *keys = [[list_dict allKeys] sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        if ([obj1 intValue] > [obj2 intValue])
            return NSOrderedDescending;
        else
            return NSOrderedAscending;
    }];
    
    for (NSString *vipLV in keys)
    {
        [list_array addObject:@{vipLV:[list_dict valueForKey:vipLV]}];
    }
    
    [new_dict setValue:[dict valueForKey:@"retcode"] forKey:@"retcode"];
    [new_dict setValue:[dict valueForKey:@"title"] forKey:@"title"];
    [new_dict setValue:list_array forKey:@"list"];
    return new_dict;
}
@end
